/**
 * typo utilities
 * Copyright (c) 2009 cmiscm.com
 * date of final modification : 2010. 02. 20
*/
package com.cmiscm.utils.typo
{
	import flash.text.TextField;
	import flash.text.TextFormat;


	/**
	 * Apply TextFormat at Linkage Textfield
	 * <p>
	 * Linkage에 있는 TextField Format을 적용시킴
	 * 
	 * @example
     * <listing>
     * package {
     *    import com.cmiscm.utils.typo.TypoUtilities;
     *    import flash.display.Sprite;
     *    import flash.text.TextField;
     *    import flash.events.Event;
     * 
     *    public class TestTypo extends Sprite
     *    {
     *        public function TestTypo()
     *        {
     *             super();
     *             this.addEventListener(Event.ADDED_TO_STAGE, onAdd);
     *         }
     * 
     *         private function onAdd(evt:Event):void
     *         {
     *             evt.currentTarget.removeEventListener(evt.type, arguments.callee);
     * 
     *             var str:String = "hellow {color:0xFF0000}World{/color}<br>Enjoy the Flash";
     * 
     *             var textfield:TextField = new TextField();
     *             TypoUtilities.setTextFormat(Linkage.textfield, textfield, str, 12, 0x000000);
     *             this.addChild(textfield);
     *        }
     *    }
     * }
     * </listing>
	 */	
	public class TypoUtilities
	{	
		/**
		 * Set text format 
		 * @param $font TextField have textformat
		 * @param $txt TextField
		 * @param $str String
		 * @param $size Font Size
		 * @param $color Font Color
		 * @param $letterSpacing Font Letter Spacing
		 * @param $multiline TextField Multiline
		 * @param $leading Font Leading
		 * @param $align Font Align
		 * @param $selectable TextField Selectable
		 * @param $antiAliasType Font AntiAliasType
		 * @param $condenseWhite Font CondenseWhite
		 * @param $mouseEnable TextField MouseEnable
		 * 
		 */
		public static function setTextFormat($font:TextField, $txt:TextField, $str:String, 
        							     	 $size:int, $color:uint, $letterSpacing:Number=0, 
        							     	 $multiline:Boolean=false, $leading:Number=0,
        							     	 $align:String="left", $selectable:Boolean=false,        							     	 
        							     	 $antiAliasType:String="nomal", $condenseWhite:Boolean=true, $mouseEnable:Boolean=false):void
        {
            var font_name:String = null;
            $txt.condenseWhite = $condenseWhite;
            $txt.autoSize = $align;
           
            var text_format:TextFormat = $font.getTextFormat();
            text_format.color = $color;		
			text_format.size = $size;
			text_format.letterSpacing = $letterSpacing;
			text_format.leading = $leading;
			
			var str:String = $str;
			
			var is_color:Boolean = false;
			if ($str.indexOf("{color:") >= 0) {
            	is_color = true;
            	var arr:Array = setColor(str);
            	var info:Array = arr[0];
            	str = arr[1];
            }
            
			$txt.multiline = $multiline;
			$txt.htmlText = str;
			$txt.embedFonts = true;
			$txt.setTextFormat(text_format);   
			
			if (is_color) {  
	            var total:int = info.length;
	            for (var i:int=0; i<total; i++) {
	            	text_format.color = info[i].color;
					$txt.setTextFormat(text_format, info[i].start, info[i].end);
	            }
	        }
            $txt.antiAliasType = $antiAliasType;
            $txt.selectable = $selectable; 
            
            $txt.mouseEnabled = $mouseEnable;
        }
		
		private static function setColor($str:String):Array
        {
        	var color_arr:Array = [];
        	var txt_str:String = "";
        	
        	var select_arr:Array = [];
        	var arr:Array = $str.split("{color");  
        	var total:int = arr.length;
        	var i:int, k:int, k_total:int;
        	for (i=0; i<total; i++) {
        		var krr:Array = arr[i].split("{/color}");
        		k_total = krr.length;
        		for(k=0; k<k_total; k++) select_arr.push(krr[k]);
           	}
        	
        	var txt_color:uint, txt:String, select_txt:String, obj:Object, br_txt:String;
        	var select_total:int = select_arr.length;
        	var check_length:int = 0;
        	for (i=0; i<select_total; i++) {
        		txt = select_arr[i];
        		if (txt.indexOf(":0x") == 0) {
        			txt_color = uint(txt.slice(1, 9));
        			select_txt = txt.slice(10);
        			obj = {start:check_length, end:check_length + select_txt.length, color:txt_color};
        			color_arr.push(obj);
        		} else {
        			select_txt = txt;
        		}
        		br_txt = select_txt.split("<br>").join(" ");
        		check_length += br_txt.length;
        		txt_str += select_txt;
        		
        	}        	
        	return [color_arr, txt_str];
        }
	}
}